package com.aricksm.xuan.activaty02;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.lang.reflect.Method;

/**
 * Created by xuan on 16/6/25.
 */
public class ImgActivity extends Activity {

    ImageView img1;
    //private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.img);

        img1 = (ImageView) findViewById(R.id.BigImg);

        //Context context = new Context();
        //context.getColor();

        Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(img1);


    }

    //菜单相关

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setIconEnable(menu, true);
        new MenuInflater(this).inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.main_menu_1:
                // 处理 第一个 菜单 的 点击事件
                break;
            case R.id.main_menu_2:
                // TODO
                break;
            case R.id.main_menu_3:

                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    //enable为true时，菜单添加图标有效，enable为false时无效。4.0系统默认无效
    private void setIconEnable(Menu menu, boolean enable) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);

            //MenuBuilder实现Menu接口，创建菜单时，传进来的menu其实就是MenuBuilder对象(java的多态特征)
            m.invoke(menu, enable);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
